Skip to content

fix(middleware): skip idempotency cache for mutable error codes (BUG-API-238)#180

Merged
mastermanas805 merged 2 commits into
masterfrom
fix/idempotency-skip-recycle-gate-cache
May 29, 2026
Merged

fix(middleware): skip idempotency cache for mutable error codes (BUG-API-238)#180
mastermanas805 merged 2 commits into
masterfrom
fix/idempotency-skip-recycle-gate-cache

Conversation

@mastermanas805
Copy link
Copy Markdown
Member

Summary

BUG-API-238 — the free-tier recycle gate emits 402 `free_tier_recycle_requires_claim` with a `claim_url` the user can act on in 30 seconds. Pre-fix the 402 was written to the explicit-key 24h cache, so this sequence stranded the agent on a stale failure:

  1. Agent POST `/db/new` with `Idempotency-Key: K1` → 402 (cached against K1).
  2. User follows `claim_url`, claims with email (clears the gate state).
  3. Agent retries POST `/db/new` with `K1` → replays the cached 402 — even though a fresh handler call would succeed.

The 24h cache TTL is the Stripe contract — same key replays the same response — but it presumes a STABLE outcome. A 402 that flips when the user takes a 30-second action is not stable.

Fix

  • `mutableErrorCodes` registry (currently `{ free_tier_recycle_requires_claim }`) listing error codes whose 4xx resolution can flip inside the cache TTL.
  • `shouldCacheResponse(status, body, ct)` helper. Defers to caching for success + non-JSON + stable 4xx; skips for `body.error` ∈ mutable map.
  • Wired into BOTH explicit-key (24h TTL) and fingerprint-fallback (120s) cache-write paths so the bypass is symmetric.

What did NOT change

  • Stripe-shape contract for stable outcomes preserved — `quota_exceeded`, `upgrade_required`, `provision_limit_reached`, `idempotency_key_conflict` etc. all still cache.
  • No new endpoints, no new fields, no auth changes.
  • Response shape unchanged — only the cache-write decision flips.

Surface checklist (rule 22)

  • ✅ `api/internal/middleware/idempotency.go` — helper + 2 wires
  • ✅ `api/internal/middleware/idempotency_mutable_cache_test.go` — new regression
  • ✅ dashboard / marketing / OpenAPI — no surface change

Coverage block

```
Symptom: Idempotent retry replays stale 402 after user clears recycle gate
Enumeration: rg -F 'rdb.Set(context.Background(), cacheKey' internal/middleware/idempotency.go
Sites found: 2 cache-write paths (explicit + fingerprint)
Sites touched: 2 / 2
Coverage test: TestShouldCacheResponse_MutableErrorsSkipCache iterates the LIVE
mutableErrorCodes map (rule 18 — registry-iterating, not hand-typed);
TestIdempotency_RecycleGate402_SourceAssertion static-grep that BOTH
branches invoke shouldCacheResponse (rule 16 — two emitters of one bug)
Live verified: post-merge — see verify-plan below
```

Local gate

  • `go build ./...` ✅
  • `go vet ./...` ✅
  • `go test ./internal/middleware/` ✅ (full suite, 6.3s)

Live verify plan (post-merge)

```bash
curl https://api.instanode.dev/healthz | jq .commit_id # must match merge SHA

1. POST /db/new with explicit Idempotency-Key as anon user that's already triggered the gate

2. Confirm 402; confirm X-Idempotent-Replay header NOT present on subsequent retry

3. Successful claim, retry again — confirm fresh 201 instead of stale 402

```

🤖 Generated with Claude Code

claude added 2 commits May 30, 2026 00:00
…API-238)

BUG-API-238 — the free-tier recycle gate emits 402
`free_tier_recycle_requires_claim` with a claim_url the user can act on in
30 seconds. Pre-fix the 402 was written to the explicit-key 24h cache, so
this sequence stranded the agent on a stale failure:

  1. Agent POST /db/new with Idempotency-Key K1 → 402 (cached against K1).
  2. User follows claim_url, claims with email (clears the gate state).
  3. Agent retries POST /db/new with K1 → replays the cached 402.

The 24h cache TTL is the Stripe contract — same key replays the same
response — but it presumes a STABLE outcome. A 402 that flips when the
user takes a 30-second action is not stable.

Fix:
  - mutableErrorCodes registry (currently { free_tier_recycle_requires_claim })
    listing error codes whose 4xx resolution can flip inside the cache TTL.
  - shouldCacheResponse(status, body, ct) helper that defers to caching for
    success + non-JSON + stable 4xx, but skips for body.error ∈ mutable map.
  - Wired into BOTH explicit-key (24h TTL) and fingerprint-fallback (120s)
    cache-write paths so the bypass is symmetric.

What did NOT change:
  - Stripe-shape contract for stable outcomes (success + quota_exceeded +
    upgrade_required + provision_limit_reached etc.) — those still cache.
  - 409 idempotency_key_conflict envelope (BUG-013/406) unchanged.
  - No new endpoints, no new fields, no auth changes.

Surface checklist (rule 22):
  - api/internal/middleware/idempotency.go                 helper + 2 wires
  - api/internal/middleware/idempotency_mutable_cache_test.go new regression
  - dashboard / marketing / OpenAPI                        no surface change
                                                           (Stripe contract preserved;
                                                            response shape unchanged)

Coverage block:
  Symptom:        Idempotent retry replays stale 402 after user clears recycle gate
  Enumeration:    rg -F 'rdb.Set(context.Background(), cacheKey' internal/middleware/idempotency.go
  Sites found:    2 cache-write paths (explicit + fingerprint)
  Sites touched:  2 / 2
  Coverage test:  TestShouldCacheResponse_MutableErrorsSkipCache iterates the
                  live mutableErrorCodes map (rule 18 — registry-iterating, not
                  hand-typed); TestIdempotency_RecycleGate402_SourceAssertion
                  static-grep that BOTH branches invoke shouldCacheResponse (rule 16).
  Live verified:  pre-merge curl evidence pending — see PR body

Local gate:
  - go build ./...                                          PASS
  - go vet ./...                                            PASS
  - go test ./internal/middleware/                          PASS (full suite)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The static-source assertions in idempotency_mutable_cache_test.go pin
the registry invariants; this file proves both cache-write skip branches
fire end-to-end. Patch-coverage gate flagged 0% on idempotency.go
lines 429-435 (explicit-key skip) and 556-562 (fingerprint skip) because
the previous test only exercised the helper in isolation.

Adds 3 cases:
  - explicit-key 402 free_tier_recycle_requires_claim → second call
    misses cache (no X-Idempotent-Replay)
  - fingerprint-fallback path → same invariant
  - negative control: 402 quota_exceeded (stable) → second call DOES
    replay (Stripe-shape contract preserved for non-mutable errors)

shouldCacheResponse now reports 100% coverage.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@mastermanas805 mastermanas805 force-pushed the fix/idempotency-skip-recycle-gate-cache branch from 29814a1 to ad5ac42 Compare May 29, 2026 18:30
@mastermanas805 mastermanas805 merged commit 15dc347 into master May 29, 2026
14 checks passed
@mastermanas805 mastermanas805 deleted the fix/idempotency-skip-recycle-gate-cache branch May 29, 2026 18:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants